package com.stx.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stx.exception.CustomException;
import com.stx.mapper.JudgeMapper;
import com.stx.mapper.MatchMapper;
import com.stx.pojo.Judge;
import com.stx.pojo.Match;
import com.stx.pojo.Ruanyue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class MatchService extends ServiceImpl<MatchMapper, Match> {

    @Autowired
    private MatchMapper matchMapper;

    public List<Match> getmatch() {
        return matchMapper.selectmatch();
    }


    public void add(Match match) {

        Match user = matchMapper.findByName(match.getMatName());
        if(user != null) {
            //说明已经有了，这里我们就要提示前台不允许新增了
            throw new CustomException("该用户名已存在，请勿重复添加");
        }

        if(match.getMatDate() == null) {
            match.setMatDate(LocalDateTime.now());
        }
        matchMapper.add(match);
    }

    public void update(Match match) {
        match.setMatDate(LocalDateTime.now());
        matchMapper.updateID(match);
    }

    public void update1(Match match) {
        match.setMatDate(LocalDateTime.now());
        matchMapper.updateID1(match);
    }

    public void delete(Integer id) {
        matchMapper.deleteID(id);
    }

    public Page<Match> find(Object name,int currentPage, int pageSize) {
        QueryWrapper<Match> queryWrapper = new QueryWrapper<>();
        //查询的条件
        queryWrapper.like(name!=null,"mat_name", name);
        //创建分页条件
        Page<Match> page = new Page<>(currentPage, pageSize);
        //执行分页查询（分页结果）
        Page<Match> resultPage = matchMapper.selectPage(page, queryWrapper);
        //当前页数据
        List<Match> records = resultPage.getRecords();
        //总记录数
        long total = resultPage.getTotal();
        //总页数
        long pages = resultPage.getPages();
        com.stx.common.Page<Match> pageFind = new com.stx.common.Page<>();
        pageFind.setRecords(records);
        pageFind.setTotal(total);
        pageFind.setPages(pages);
        return page;
    }

    public List<Match> listByRuanyueId(Integer ruanyueId) {
        LambdaQueryWrapper<Match> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Match::getRuaId, ruanyueId);
        wrapper.orderByAsc(Match::getMatSequence);
        return baseMapper.selectList(wrapper);
    }

    public Map<Integer,String> listMatch(){
        List<Match> matches = matchMapper.selectList(null);
        Map<Integer, String> collect = matches.stream().collect(Collectors.toMap(Match::getId, Match::getMatName));
        return collect;
    }
}
